package org.microservice.common.handler;

import com.baomidou.mybatisplus.mapper.MetaObjectHandler;
import org.apache.ibatis.reflection.MetaObject;
import org.microservice.common.util.AuthUtil;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * Created with IntelliJ IDEA.
 * User: 齐丹枫
 * Date: 2018/7/2
 * Time: 12:33
 * To change this template use File | Settings | File Templates.
 * Description:
 */
@Component
public class MyMetaObjectHandler extends MetaObjectHandler {

    DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");


    @Override
    public void insertFill(MetaObject metaObject) {
        //int id = AuthUtil.getUserId();
        if(metaObject.hasSetter("createdBy")){
            setFieldValByName("createdBy",AuthUtil.getUserName(),metaObject);
        }

        if(metaObject.hasSetter("createdTime")){
            setFieldValByName("createdTime", dateTimeFormatter.format(LocalDateTime.now()).toString(),metaObject);
        }


    }

    @Override
    public void updateFill(MetaObject metaObject) {
        if(metaObject.hasSetter("lastModifiedBy")){
            setFieldValByName("lastModifiedBy",AuthUtil.getUserName(),metaObject);
        }
        if(metaObject.hasSetter("lastModifiedTime")){
            setFieldValByName("lastModifiedTime",dateTimeFormatter.format(LocalDateTime.now()).toString(),metaObject);
        }


    }
}
